package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhentao.mapper.ApUserMapper;
import com.zhentao.model.user.dto.LoginDto;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.user.pojo.ApUser;
import com.zhentao.service.ApUserService;
import com.zhentao.utils.common.AppJwtUtil;
import com.zhentao.utils.common.Base64Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class ApUserServiceImpl implements ApUserService {
    @Autowired
    private ApUserMapper apUserMapper;
    @Override
    public ResponseResult login(LoginDto loginDto) {
//        验证参数，参数不能未空
        if(!StringUtils.isBlank(loginDto.getPhone())||!StringUtils.isBlank(loginDto.getPassword())){
            LambdaQueryWrapper<ApUser> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(ApUser::getPhone, loginDto.getPhone());
            ApUser apUser = apUserMapper.selectOne(lambdaQueryWrapper);
            if(apUser==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
            }
//            获取盐
            String salt = apUser.getSalt();
//            获取密码
            String old_password = apUser.getPassword();
            String new_password = DigestUtils.md5DigestAsHex((loginDto.getPassword() + salt).getBytes());
//            密码是否正确
            if(!old_password.equals(new_password)){
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
            Map<String,Object> map = new HashMap<>();
            map.put("token", AppJwtUtil.getToken(apUser.getId().longValue()));
//           不穿密码和盐
            apUser.setPassword("");
            apUser.setSalt("");
            map.put("user", apUser);
            return ResponseResult.okResult(map);

        }else {//游客登录

            Map<String,Object> map = new HashMap<>();
            map.put("token", AppJwtUtil.getToken(0l));
            return ResponseResult.okResult(map);
        }
    }

    @Override
    public ResponseResult sava(ApUser user) {
        if(StringUtils.isBlank(user.getName())){
            return ResponseResult.errorResult(400,"姓名不能为空");
        }
        if(StringUtils.isBlank(user.getPhone())){
            return ResponseResult.errorResult(400,"手机号不能为空");
        }
        if(user.getPhone().length()!=11){
            return ResponseResult.errorResult(400,"手机号长度不满11位");
        }
        ApUser apUser = apUserMapper.selectOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getPhone, user.getPhone()));
        if(apUser!=null){
            return ResponseResult.errorResult(400,"手机号已经注册，请重新注册");
        }
        if(StringUtils.isBlank(user.getPassword())){
            return ResponseResult.errorResult(400,"密码不能为空");
        }

        String salt="abcd";
        String password = DigestUtils.md5DigestAsHex((user.getPassword() + salt).getBytes());
        user.setPassword(password);
        user.setSalt(salt);
        user.setCreatedTime(new Date());
        apUserMapper.insert(user);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
